package com.ts.proj.service.impl;

import com.ts.proj.entity.StorageEntity;
import com.ts.proj.repository.StorageRepository;
import com.ts.proj.service.StorageService;

import com.ts.base.service.impl.CommonServiceImpl;

import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


//说明 字符串：，作为替换字符串 LOWERStorage                Storage

@Service
public class StorageServiceImpl extends CommonServiceImpl<StorageEntity>  implements StorageService {

    @Autowired
    private StorageRepository storageRepository;

    @Override
    public List<StorageEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<StorageEntity> sp = this.getSpecification(pageData,StorageEntity.class);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return storageRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count(PageData pageData) {
        Specification<StorageEntity> sp = this.getSpecification(pageData,StorageEntity.class);
        return storageRepository.count(sp);
    }

    @Override
    public boolean deleteByIds(Iterable<StorageEntity> iterable) {
        storageRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public List<StorageEntity> saveAll(List<StorageEntity> entities) {
        return storageRepository.saveAll(entities);
    }

    @Override
    public List<StorageEntity> findAllById(Integer[] ids) {
        List<StorageEntity> list = storageRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            StorageEntity entity = new StorageEntity();
            list.add(entity);
        }
        return list;
    }

    @Override
    public boolean deleteAll() {
		storageRepository.deleteAll();
        return false;
    }
}